package com.demo.test.mongodb;

import com.demo.mapper.StudentRepository;
import com.demo.test.mongodb.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

//@Component
public class StudentDataLoader implements CommandLineRunner {

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void run(String... args) throws Exception {
        // 创建一些学生数据
        List<Student> students = Arrays.asList(
                new Student("Alice", 85, "A"),
                new Student("Bob", 72, "B"),
                new Student("Charlie", 65, "A"),
                new Student("David", 92, "B"),
                new Student("Eve", 55, "C"),
                new Student("Frank", 78, "C"),
                new Student("Grace", 88, "A"),
                new Student("Hannah", 47, "B")
        );

        // 清空现有数据（可选）
        mongoTemplate.dropCollection(Student.class);

        // 批量插入学生数据
        studentRepository.saveAll(students);
        System.out.println("Data imported successfully!");

        // 打印导入的所有学生数据（可选）
        studentRepository.findAll().forEach(System.out::println);
    }
}
